package cn.moukou.tongji.ihealth.auth.service;

import cn.moukou.tongji.ihealth.auth.dao.AdminMapper;
import cn.moukou.tongji.ihealth.auth.dao.DoctorMapper;
import cn.moukou.tongji.ihealth.auth.dao.PatientMapper;
import cn.moukou.tongji.ihealth.auth.pojo.Admin;
import cn.moukou.tongji.ihealth.auth.pojo.Doctor;
import cn.moukou.tongji.ihealth.auth.pojo.Patient;
import cn.moukou.tongji.ihealth.auth.util.Md5Util;
import cn.moukou.tongji.ihealth.core.bean.Resp;
import cn.moukou.tongji.ihealth.core.util.JwtUtil;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AuthService {

    @Autowired
    DoctorMapper doctorMapper;

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    PatientMapper patientMapper;

    public Resp<Object> authDoctor(String id, String password) {
        Map<String, Object> resMap = new HashMap<>();
        Doctor doctor = doctorMapper.selectById(id);
        if (doctor == null) {
            //账号不存在
            return Resp.fail("用户不存在或密码不正确");
        }
        if(!doctor.getPassword().equals(Md5Util.getMD5String(password))) {
            //密码错误
            return Resp.fail("用户不存在或密码不正确");
        }

        resMap.put("userInfo", doctor);
        resMap.put("role", "doctor");

        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("userInfo", doctor);
        tokenMap.put("role", "doctor");
        try {
            String token = JwtUtil.generateToken(tokenMap, 5);
            resMap.put("token", token);
            return Resp.ok(resMap);

        } catch (Exception e) {
            e.printStackTrace();
            return Resp.fail("服务器生成token异常");
        }
    }

    public Resp<Object> authAdmin(String id, String password) {
        Map<String, Object> resMap = new HashMap<>();
        Admin admin = adminMapper.selectById(id);
        if(admin == null) {
            //账号不存在
            return Resp.fail("用户不存在或密码不正确");
        }
        if(!admin.getPassword().equals(Md5Util.getMD5String(password))) {
            //密码错误
            return Resp.fail("用户不存在或密码不正确");
        }
        resMap.put("userInfo", admin);
        resMap.put("role", "admin");

        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("userInfo", admin);
        tokenMap.put("role", "admin");
        try {
            String token = JwtUtil.generateToken(tokenMap, 5);
            resMap.put("token", token);
            return Resp.ok(resMap);

        } catch (Exception e) {
            e.printStackTrace();
            return Resp.fail("服务器生成token异常");
        }
    }

    public Resp<Object> authPatient(String id, String password) {
        Map<String, Object> resMap = new HashMap<>();
        Patient patient = patientMapper.selectById(id);
        if(patient == null) {
            //账号不存在
            return Resp.fail("用户不存在或密码不正确");
        }
        if(!patient.getPassword().equals(Md5Util.getMD5String(password))) {
            //密码错误
            return Resp.fail("用户不存在或密码不正确");
        }
        resMap.put("userInfo", patient);
        resMap.put("role", "patient");

        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("userInfo", patient);
        tokenMap.put("role", "patient");

        try {
            String token = JwtUtil.generateToken(tokenMap, 5);
            resMap.put("token", token);
            return Resp.ok(resMap);

        } catch (Exception e) {
            e.printStackTrace();
            return Resp.fail("服务器生成token异常");
        }
    }


}
